package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/10/14 11:48
 * BMW F750GS
 **/
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;
    /**
     *搜索商品
     */
    @GetMapping("list.html")
    public String toList(Model model, SearchParam searchParam){
        //根据用户输入的关键字匹配商品分类-->例如 手机, 电脑
        //跟据商品分类查询对应的 平台属性值
            // 品牌:
            //CPU型号:
            //运行内存:
            //机身存储:
        //根据商品分类 查询对应的 商品

        SearchResponseVo responseVo = listFeignClient.search(searchParam);
        //1:回显关键词
        model.addAttribute("searchParam", searchParam);
        //2:品牌集合
        model.addAttribute("trademarkList", responseVo.getTrademarkList());
        //3:urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //4:平台属性
        model.addAttribute("attrsList", responseVo.getAttrsList());
        //5:商品集合
        model.addAttribute("goodsList", responseVo.getGoodsList());
        //6: 总页数  当前页
        model.addAttribute("pageNo", responseVo.getPageNo());
        model.addAttribute("totalPages", responseVo.getTotalPages());
        //7:排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);

        //8:条件回显
        //8.1 显示品牌  //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            model.addAttribute("trademarkParam", "品牌:" + trademark.split(":")[1]);
        }
        //8.2:显示平台属性
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);
        return "list/index";
    }
    //显示平台属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        //平台属性数组
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                //props=23:4G:运行内存 prop
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());

        }
        return null;
    }

    //排序
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        // order=1:asc  排序规则   2:asc
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] o = order.split(":");
            orderMap.put("type", o[0]);
            orderMap.put("sort", o[1]);
        } else {
            //默认 排序  hotScore  desc
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");
        }
        return orderMap;
    }

    //追加URL
    private String buildUrlParam(SearchParam searchParam) {
        //企业上班使用 StringBuilder  比 StringBuffer 快 效率高
        //企业上班使用 StringBuilder  比 StringBuffer 不安全
        // Spring容器
        StringBuilder sb = new StringBuilder();
        // sb.append("http://list.gmall.com/list.html?");
        //1:KeyWord
        String keyword = searchParam.getKeyword();
        sb.append("http://list.gmall.com/list.html?keyword=" + keyword);
        //2:品牌  品牌的Id:品牌的名称
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=" + trademark);
        }
        //3:平台属性
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=" + prop);
            }
        }
        //4：一二三级分类  暂时不写了
        return sb.toString();
    }
}
